﻿class Example
{

}
class P1
{
    public Action<string> action;
    public string name;
    public int age;

    public P1(string name, int age)
    {
        this.name = name;
        this.age = age;
    }
    public void wcs()
    {
        Console.WriteLine("我是无参数的电脑需要: " + "1848482167841" + "$");
    }
    public void cmp1(float money)
    {
        Console.WriteLine("电脑1需要: " + money + "$");
    }
    public void cmp2(float money)
    {
        Console.WriteLine("电脑2需要: " + money + "$");
    }
    public void cmp3(float money)
    {
        Console.WriteLine("电脑3需要: " + money + "$");
    }

    public string Func1()
    {
        return "有返回值但是无参数";
    }

    public Example Func2(string name)
    {
        Console.WriteLine("有返回值，也有参数:" + name);
        return null;
    }
    public void IsAction(string age)
    {
        Console.WriteLine("我是一个电脑维修师: " + age);
        if (action != null)
        {
            action(age);
            Console.WriteLine("我是一个电脑维修师: " + age);
        }
    }
}

class Program
{
    public static void read(string name)
    {
        Console.WriteLine(name);
        Console.BackgroundColor = ConsoleColor.Red;
        Console.ForegroundColor = ConsoleColor.Black;
    }
    static void Main(string[] args)
    {
        P1 p = new P1("先生", 18);
        //匿名委托
        p.action = delegate (string value)
        {
            Console.WriteLine(value);
            Console.WriteLine("我好懵啊2");
        };
        p.action += read;
        p.IsAction("我好懵啊1");

        Console.BackgroundColor = ConsoleColor.Black;
        Console.ForegroundColor = ConsoleColor.White;

        //下面使用系统委托
        //无返回值的系统委托
        //1.无参数的
        Action action;
        action = p.wcs;
        action();

        //2.有参数的
        Action<float> a1;
        a1 = p.cmp1;
        a1 += p.cmp2;
        a1 += p.cmp3;
        a1(182);

        //有返回值的系统委托
        //有返回值但是无参数
        Func<string> func;
        func = p.Func1;
        Console.WriteLine(func());

        //有返回值且有参数
        Func<string, Example> func1;
        func1 = p.Func2;
        Example a = func1("先生");
        Console.WriteLine(a);
    }

}

